#include "stdafx.h"
#include "wf_drawtext.h"

#include "pf_snapper.h"
#include "pf_graphicview.h"



WF_DrawText::WF_DrawText(PF_Container& container,
                                     PF_GraphicView& graphicView)
        :PF_PreviewActionInterface("Draw Text",
                           container, graphicView) {

    //text = NULL;
    pos = PF_Vector(false);
    textChanged = true;
}


QAction* WF_DrawText::createGUIAction(LX_Define::ActionType /*type*/, QObject* /*parent*/) {
	// tr("Text")
    QAction* action = new QAction(tr("&Text"),  NULL);
	action->setIcon(QIcon(":/extui/menutext.png"));
    //action->zetStatusTip(tr("Draw Text Entities"));
    return action;
}


void WF_DrawText::init(int status) {
    PF_ActionInterface::init(status);
    if (PF_DIALOGFACTORY!=NULL) {

        switch (status) {
        case ShowDialog: {
                reset();

                PF_Text tmp(NULL, data);
                if (PF_DIALOGFACTORY->requestTextDialog(&tmp)) {
                    data = tmp.getData();
                    preparePreview();
                    preview->setVisible(false);

                    setStatus(SetPos);
                    showOptions();
                } else {
                    hideOptions();
                    finish();
                }
			//   should we reall redraw here??
			graphicView->redraw(LX_Define::RedrawDrawing); 

            }
            break;

        case SetPos:
            PF_DIALOGFACTORY->requestOptions(this, true, true);
            break;

        default:
            break;
        }
    }
}



void WF_DrawText::reset() {
    data = PF_TextAttribute(PF_Vector(0.0,0.0),
                       1.0, 100.0,
                       LX_Define::VAlignTop,
                       LX_Define::HAlignLeft,
                       LX_Define::LeftToRight,
                       LX_Define::Exact,
                       1.0,
                       data.text,
                       "standard",
                       0.0,
                       LX_Define::Update);
}



void WF_DrawText::trigger() {

    PF_DEBUG->print("PF_ActionDrawText::trigger()");

    if (pos.valid) {
        deletePreview();

        PF_Text* text = new PF_Text(container, data);
        text->update();
        container->addEntity(text);

        if (document) {
            document->startUndoCycle();
            document->addUndoable(text);
            document->endUndoCycle();
        }

		graphicView->redraw(LX_Define::RedrawDrawing);

        textChanged = true;
        setStatus(SetPos);
    }
}


void WF_DrawText::preparePreview() {
    data.insertionPoint = PF_Vector(0.0,0.0);
    PF_Text* text = new PF_Text(preview, data);
    text->update();
    //text->setVisible(false);
    preview->addEntity(text);
    textChanged = false;
}


void WF_DrawText::mouseMoveEvent(PF_MouseEvent* e) {
    PF_DEBUG->print("PF_ActionDrawText::mouseMoveEvent begin");

    if (getStatus()==SetPos) {
        PF_Vector mouse = snapPoint(e);
        pos = mouse;

        deletePreview();
        if (textChanged) {
            preparePreview();
        }
        if (!preview->isVisible()) {
            preview->setVisible(true);
        }
        offset = PF_Vector(graphicView->toGuiDX(pos.x),
                           -graphicView->toGuiDY(pos.y));
        drawPreview();
    }

    PF_DEBUG->print("PF_ActionDrawText::mouseMoveEvent end");
}



void WF_DrawText::mouseReleaseEvent(PF_MouseEvent* e) {
    if (e->button()==Qt::LeftButton) {
        PF_CoordinateEvent ce(snapPoint(e));
        coordinateEvent(&ce);
    } else if (e->button()==Qt::RightButton) {
        deletePreview();
        //init(getStatus()-1);
        finish();
    }
}



void WF_DrawText::coordinateEvent(PF_CoordinateEvent* e) {
    if (e==NULL) {
        return;
    }

    PF_Vector mouse = e->getCoordinate();

    switch (getStatus()) {
    case ShowDialog:
        break;

    case SetPos:
        data.insertionPoint = mouse;
        trigger();
        break;

    default:
        break;
    }
}



void WF_DrawText::commandEvent(PF_CommandEvent* e) {
    PF_String c = e->getCommand().lower();

    if (checkCommand("help", c)) {
        if (PF_DIALOGFACTORY!=NULL) {
            PF_DIALOGFACTORY->commandMessage(msgAvailableCommands()
                                             + getAvailableCommands().join(", "));
        }
        return;
    }

    switch (getStatus()) {
    case SetPos:
        if (checkCommand("text", c)) {
            deletePreview();
            graphicView->disableCoordinateInput();
            setStatus(SetText);
        }
        break;

    case SetText: {
            setText(e->getCommand());
            if (PF_DIALOGFACTORY!=NULL) {
                PF_DIALOGFACTORY->requestOptions(this, true, true);
            }
            graphicView->enableCoordinateInput();
            setStatus(SetPos);
        }
        break;

    default:
        break;
    }
}



PF_StringList WF_DrawText::getAvailableCommands() {
    PF_StringList cmd;
    if (getStatus()==SetPos) {
        cmd += command("text");
    }
    return cmd;
}



void WF_DrawText::showOptions() {
    PF_ActionInterface::showOptions();

    if (PF_DIALOGFACTORY!=NULL) {
        PF_DIALOGFACTORY->requestOptions(this, true, true);
    }
}



void WF_DrawText::hideOptions() {
    PF_ActionInterface::hideOptions();

    if (PF_DIALOGFACTORY!=NULL) {
        PF_DIALOGFACTORY->requestOptions(this, false);
    }
}



void WF_DrawText::updateMouseButtonHints() {
    if (PF_DIALOGFACTORY!=NULL) {
        switch (getStatus()) {
        case SetPos:
            PF_DIALOGFACTORY->updateMouseWidget(tr("Specify insertion point"),
                                                tr("Cancel"));
            break;
        case SetText:
            PF_DIALOGFACTORY->updateMouseWidget(tr("Enter text:"),
                                                tr("Back"));
            break;
        default:
            PF_DIALOGFACTORY->updateMouseWidget("", "");
            break;
        }
    }
}



void WF_DrawText::updateMouseCursor() {
    graphicView->setMouseCursor(LX_Define::CadCursor);
}



void WF_DrawText::updateToolBar() {
    if (PF_DIALOGFACTORY!=NULL) {
        switch (getStatus()) {
        case SetPos:
            PF_DIALOGFACTORY->requestToolBar(LX_Define::ToolBarSnap);
            break;
        default:
            PF_DIALOGFACTORY->requestToolBar(LX_Define::ToolBarMain);
            break;
        }
    }
}



void WF_DrawText::setText(const PF_String& t) {
    data.text = t;
    textChanged = true;
}



PF_String WF_DrawText::getText() {
    return data.text;
}


void WF_DrawText::setAngle(double a) {
    data.angle = a;
    textChanged = true;
}

double WF_DrawText::getAngle() {
    return data.angle;
}


// EOF
